package megaezgo

import (
	//"wxsdk/utils/mapstruct"
	"github.com/astaxie/beego"
)

type PayRsp struct {
	SYS_ORDER_NO string `json:"SYS_ORDER_NO"`
	APPID        string `json:"APPID"`
	TIME_STAMP   string `json:"TIME_STAMP"`
	NONCESTR     string `json:"NONCESTR"`
	PACKAGE      string `json:"PACKAGE"`
	SIGN_TYPE    string `json:"SIGN_TYPE"`
	SIGN         string `json:"SIGN"`
}

/*
ok:
{"Header":{"StatusCode":"0000","StatusDesc":"執行成功","ResponseTime":"2019-02-13 18:14:08"},"Data":{"DataValue":"{\"SYS_ORDER_NO\":\"2019021300016774\",\"APPID\":\"wxfed5e436d876b7b8\",\"TIME_STAMP\":\"1550052848\",\"NONCESTR\":\"12c67122a53047b2a607a572bc8737de\",\"PACKAGE\":\"prepay_id=wx13181408232697ea455ae8c53979191223\",\"SIGN_TYPE\":\"MD5\",\"SIGN\":\"AAD56BC64A9C129FD8A07CC5D1B04781\"}"}}

error:

*/

func MicroAppPay(apiStoreId,terminalIp,apiStorePw,appId,openId,orderId,barcodeSeq,
                 currency,purposeType string,amount int,privateKey []byte)(rsp *PayRsp, err error){
	
	type PayReq struct {
		StoreOrderNo  string `json:"StoreOrderNo"`
		BarcodeSeq    string `json:"BarcodeSeq"`
		OpenId        string `json:"OpenId"`
		AppId         string `json:"AppId"`
		TradeCurrency string `json:"TradeCurrency"`
		TradeAmount   int    `json:"TradeAmount"`
		PurposeType   string `json:"PurposeType"`
	}
	req := new(PayReq)
	req.StoreOrderNo = orderId
	req.BarcodeSeq = barcodeSeq
	req.OpenId = openId
	req.AppId = appId
	req.TradeCurrency = currency
	req.TradeAmount = amount
	req.PurposeType = purposeType
	reqURL := "https://www.megaezgo.com/wxapi/api/WeiXin"
	sdk := NewSDK(apiStoreId,terminalIp,reqURL,apiStorePw,privateKey)
	rsp = new(PayRsp)
	_, err = sdk.PayJsonExcute("MiniProgramPay",req, rsp)
	if err != nil {
		beego.Debug("Megaezgo MicroAppPay err:", err.Error())
		return
	}
	return
}
